package com.ruoyi.project.data.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.security.service.PermissionService;
import com.ruoyi.project.data.domain.*;
import com.ruoyi.project.data.service.IWeightCarEnterService;
import com.ruoyi.project.data.service.IWeightStationService;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysUser;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.data.service.IWeightRecordService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 称重记录Controller
 *
 * @author ruoyi
 * @date 2020-03-21
 */
@RestController
@RequestMapping("/system/record")
public class WeightRecordController extends BaseController
{
    @Value("${weight.api}")
    private String weightApiUrl;

    @Autowired
    private IWeightRecordService weightRecordService;
    @Autowired
    private IWeightStationService weightStationService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private IWeightCarEnterService weightCarEnterService;

    /**
     * 查询称重记录列表
     */
    @PreAuthorize("@ss.hasPermi('weight:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(WeightRecord weightRecord)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        weightRecord.setStationId(stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        weightRecord.setAreas(areas);
        weightRecord.setManagementCharacters(managementCharacters);
        startPage();
        List<WeightRecord> list = weightRecordService.selectWeightRecordList(weightRecord);
        if(list != null && list.size() > 0) {
            for(WeightRecord record : list) {
                Map<String, Object> carMap = new HashMap<String, Object>();
                carMap.put("carNo", record.getPlantNo());
                if(record.getPlantColor() != null) {
                    String plantColor = record.getPlantColor();
                    if("1".equals(plantColor)) {
                        record.setPlantColor("蓝");
                        carMap.put("carColor", "B");
                    }else if("2".equals(plantColor)) {
                        record.setPlantColor("黄");
                        carMap.put("carColor", "Y");
                    }else if("3".equals(plantColor)) {
                        record.setPlantColor("黑");
                        carMap.put("carColor", "H");
                    }else if("4".equals(plantColor)) {
                        record.setPlantColor("白");
                        carMap.put("carColor", "W");
                    }else if("6".equals(plantColor)) {
                        record.setPlantColor("黄绿");
                        carMap.put("carColor", "D");
                    }else if("7".equals(plantColor)) {
                        record.setPlantColor("农黄");
                        carMap.put("carColor", "5");
                    }else if("8".equals(plantColor)) {
                        record.setPlantColor("绿");
                        carMap.put("carColor", "G");
                    }else if("9".equals(plantColor)) {
                        record.setPlantColor("其他");
                        carMap.put("carColor", "5");
                    }else if("92".equals(plantColor)) {
                        record.setPlantColor("农绿");
                        carMap.put("carColor", "5");
                    }else if("94".equals(plantColor)) {
                        record.setPlantColor("渐变绿");
                        carMap.put("carColor", "5");
                    }
                }
//                Double CoTotalMass = 0.0;
//                int CoAxleNumber = 0;
//                // 根据车牌号码、车牌颜色获取车辆信息
//                Map<String, Object> car = weightRecordService.getCarDataByNo(carMap);
//                if(car != null) {
//                    if(car.get("CoTotalMass") != null && !"".equals(car.get("CoTotalMass"))) {
//                        CoTotalMass += Double.parseDouble(car.get("CoTotalMass").toString());
//                    }
//                    if(car.get("CoAxleNumber") != null && !"".equals(car.get("CoAxleNumber"))) {
//                        if(StringUtils.isDigit(car.get("CoAxleNumber").toString())) {
//                            CoAxleNumber += Double.parseDouble(car.get("CoAxleNumber").toString());
//                        }
//                    }
//                }
                if(record.getPlantNoGua() != null) {
                    if(!record.getPlantNoGua().equals(record.getPlantNo())) {
//                        carMap = new HashMap<String, Object>();
//                        if(!record.getPlantNoGua().toString().contains("挂")) {
//                            carMap.put("carNo", record.getPlantNoGua()+"挂");
//                        }
//                        carMap.put("carColor", "Y");
//                        // 根据车牌号码、车牌颜色获取车辆信息
//                        car = weightRecordService.getCarDataByNo(carMap);
//                        if(car != null) {
//                            if(car.get("CoTotalMass") != null && !"".equals(car.get("CoTotalMass"))) {
//                                CoTotalMass += Double.parseDouble(car.get("CoTotalMass").toString());
//                            }
//                            if(car.get("CoAxleNumber") != null && !"".equals(car.get("CoAxleNumber"))) {
//                                if(StringUtils.isDigit(car.get("CoAxleNumber").toString())) {
//                                    CoAxleNumber += Double.parseDouble(car.get("CoAxleNumber").toString());
//                                }
//                            }
//                        }
                        if("-".equals(record.getPlantNoGua()) || "无牌车".equals(record.getPlantNoGua())){
                            record.setPlantNoGua("无");
                        }
                    }else {
                        record.setPlantNoGua("无");
                    }
                } else {
                    record.setPlantNoGua("无");
                }
//                if(CoTotalMass > 0) {
//                    record.setCoTotalMass(CoTotalMass);
//                }
//                if(CoAxleNumber > 0) {
//                    record.setCoAxleNumber(CoAxleNumber);
//                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出称重记录列表
     */
    @PreAuthorize("@ss.hasPermi('weight:record:export')")
    @Log(title = "称重记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WeightRecord weightRecord)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        weightRecord.setStationId(stationId);
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        weightRecord.setAreas(areas);
        weightRecord.setManagementCharacters(managementCharacters);
        List<WeightRecord> list = weightRecordService.selectWeightRecordList(weightRecord);
        if(list != null && list.size() > 0) {
            for(WeightRecord record : list) {
                Map<String, Object> carMap = new HashMap<String, Object>();
                carMap.put("carNo", record.getPlantNo());
                if(record.getPlantColor() != null) {
                    String plantColor = record.getPlantColor();
                    if("1".equals(plantColor)) {
                        record.setPlantColor("蓝");
                        carMap.put("carColor", "B");
                    }else if("2".equals(plantColor)) {
                        record.setPlantColor("黄");
                        carMap.put("carColor", "Y");
                    }else if("3".equals(plantColor)) {
                        record.setPlantColor("黑");
                        carMap.put("carColor", "H");
                    }else if("4".equals(plantColor)) {
                        record.setPlantColor("白");
                        carMap.put("carColor", "W");
                    }else if("6".equals(plantColor)) {
                        record.setPlantColor("黄绿");
                        carMap.put("carColor", "D");
                    }else if("7".equals(plantColor)) {
                        record.setPlantColor("农黄");
                        carMap.put("carColor", "5");
                    }else if("8".equals(plantColor)) {
                        record.setPlantColor("绿");
                        carMap.put("carColor", "G");
                    }else if("9".equals(plantColor)) {
                        record.setPlantColor("其他");
                        carMap.put("carColor", "5");
                    }else if("92".equals(plantColor)) {
                        record.setPlantColor("农绿");
                        carMap.put("carColor", "5");
                    }else if("94".equals(plantColor)) {
                        record.setPlantColor("渐变绿");
                        carMap.put("carColor", "5");
                    }
                }
//                Double CoTotalMass = 0.0;
//                int CoAxleNumber = 0;
//                // 根据车牌号码、车牌颜色获取车辆信息
//                Map<String, Object> car = weightRecordService.getCarDataByNo(carMap);
//                if(car != null) {
//                    if(car.get("CoTotalMass") != null && !"".equals(car.get("CoTotalMass"))) {
//                        CoTotalMass += Double.parseDouble(car.get("CoTotalMass").toString());
//                    }
//                    if(car.get("CoAxleNumber") != null && !"".equals(car.get("CoAxleNumber"))) {
//                        if(StringUtils.isDigit(car.get("CoAxleNumber").toString())) {
//                            CoAxleNumber += Double.parseDouble(car.get("CoAxleNumber").toString());
//                        }
//                    }
//                }
                if(record.getPlantNoGua() != null) {
                    if(!record.getPlantNoGua().equals(record.getPlantNo())) {
//                        carMap = new HashMap<String, Object>();
//                        if(!record.getPlantNoGua().toString().contains("挂")) {
//                            carMap.put("carNo", record.getPlantNoGua()+"挂");
//                        }
//                        carMap.put("carColor", "Y");
//                        // 根据车牌号码、车牌颜色获取车辆信息
//                        car = weightRecordService.getCarDataByNo(carMap);
//                        if(car != null) {
//                            if(car.get("CoTotalMass") != null && !"".equals(car.get("CoTotalMass"))) {
//                                CoTotalMass += Double.parseDouble(car.get("CoTotalMass").toString());
//                            }
//                            if(car.get("CoAxleNumber") != null && !"".equals(car.get("CoAxleNumber"))) {
//                                if(StringUtils.isDigit(car.get("CoAxleNumber").toString())) {
//                                    CoAxleNumber += Double.parseDouble(car.get("CoAxleNumber").toString());
//                                }
//                            }
//                        }
                        if("-".equals(record.getPlantNoGua()) || "无牌车".equals(record.getPlantNoGua())){
                            record.setPlantNoGua("无");
                        }
                    }else {
                        record.setPlantNoGua("无");
                    }
                } else {
                    record.setPlantNoGua("无");
                }
//                if(CoTotalMass > 0) {
//                    record.setCoTotalMass(CoTotalMass);
//                }
//                if(CoAxleNumber > 0) {
//                    record.setCoAxleNumber(CoAxleNumber);
//                }
            }
        }
        ExcelUtil<WeightRecord> util = new ExcelUtil<WeightRecord>(WeightRecord.class);
        return util.exportExcel(list, "record");
    }

    /**
     * 获取称重记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('weight:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        WeightRecord record = weightRecordService.selectWeightRecordById(id);
        if(record != null) {
            if(record.getzPlatePhoto() != null && !"".equals(record.getzPlatePhoto())) {
                if (!record.getzPlatePhoto().contains("ycsjoss.oss-cn-hangzhou.aliyuncs.com")){
                    record.setzPlatePhoto(weightApiUrl + record.getzPlatePhoto());
                }
            }else{
                // 从进出场获取图片
                WeightCarEnter carEnter = weightCarEnterService.selectCarEnterByWeight(record);

                if(carEnter != null && StringUtils.isNotEmpty(carEnter.getFrontPlatePhoto())){
                    record.setzPlatePhoto(carEnter.getFrontPlatePhoto());
                }
            }
            if(record.getRearSidePhoto() != null && !"".equals(record.getRearSidePhoto())) {
                if (!record.getRearSidePhoto().contains("ycsjoss.oss-cn-hangzhou.aliyuncs.com")){
                    record.setRearSidePhoto(weightApiUrl + record.getRearSidePhoto());
                }
            }
            Map<String, Object> carMap = new HashMap<String, Object>();
            carMap.put("carNo", record.getPlantNo());
            if(record.getPlantColor() != null) {
                String plantColor = record.getPlantColor();
                if("1".equals(plantColor)) {
                    record.setPlantColor("蓝");
                    carMap.put("carColor", "B");
                }else if("2".equals(plantColor)) {
                    record.setPlantColor("黄");
                    carMap.put("carColor", "Y");
                }else if("3".equals(plantColor)) {
                    record.setPlantColor("黑");
                    carMap.put("carColor", "H");
                }else if("4".equals(plantColor)) {
                    record.setPlantColor("白");
                    carMap.put("carColor", "W");
                }else if("6".equals(plantColor)) {
                    record.setPlantColor("黄绿");
                    carMap.put("carColor", "D");
                }else if("7".equals(plantColor)) {
                    record.setPlantColor("农黄");
                    carMap.put("carColor", "5");
                }else if("8".equals(plantColor)) {
                    record.setPlantColor("绿");
                    carMap.put("carColor", "G");
                }else if("9".equals(plantColor)) {
                    record.setPlantColor("其他");
                    carMap.put("carColor", "5");
                }else if("92".equals(plantColor)) {
                    record.setPlantColor("农绿");
                    carMap.put("carColor", "5");
                }else if("94".equals(plantColor)) {
                    record.setPlantColor("渐变绿");
                    carMap.put("carColor", "5");
                }
            }

            if(record.getGoodsCode() == null || "".equals(record.getGoodsCode())){
                record.setGoodsCode("无");
            }
//            Double CoTotalMass = 0.0;
//            int CoAxleNumber = 0;
//            // 根据车牌号码、车牌颜色获取车辆信息
//            Map<String, Object> car = weightRecordService.getCarDataByNo(carMap);
//            if(car != null) {
//                if(car.get("CoTotalMass") != null && !"".equals(car.get("CoTotalMass"))) {
//                    CoTotalMass += Double.parseDouble(car.get("CoTotalMass").toString());
//                }
//                if(car.get("CoAxleNumber") != null && !"".equals(car.get("CoAxleNumber"))) {
//                    if(StringUtils.isDigit(car.get("CoAxleNumber").toString())) {
//                        CoAxleNumber += Double.parseDouble(car.get("CoAxleNumber").toString());
//                    }
//                }
//            }
            if(record.getPlantNoGua() != null) {
                if(!record.getPlantNoGua().equals(record.getPlantNo())) {
//                    carMap = new HashMap<String, Object>();
//                    if(!record.getPlantNoGua().toString().contains("挂")) {
//                        carMap.put("carNo", record.getPlantNoGua()+"挂");
//                    }
//                    carMap.put("carColor", "Y");
//                    // 根据车牌号码、车牌颜色获取车辆信息
//                    car = weightRecordService.getCarDataByNo(carMap);
//                    if(car != null) {
//                        if(car.get("CoTotalMass") != null && !"".equals(car.get("CoTotalMass"))) {
//                            CoTotalMass += Double.parseDouble(car.get("CoTotalMass").toString());
//                        }
//                        if(car.get("CoAxleNumber") != null && !"".equals(car.get("CoAxleNumber"))) {
//                            if(StringUtils.isDigit(car.get("CoAxleNumber").toString())) {
//                                CoAxleNumber += Double.parseDouble(car.get("CoAxleNumber").toString());
//                            }
//                        }
//                    }
                    if("-".equals(record.getPlantNoGua())){
                        record.setPlantNoGua("无");
                    }
                    if("无牌车".equals(record.getPlantNoGua()) || "无车牌".equals(record.getPlantNoGua())){
                        record.setPlantNoGua("未识别");
                    }
                }else {
                    record.setPlantNoGua("无");
                }
            } else {
                record.setPlantNoGua("无");
            }
//            if(CoTotalMass > 0) {
//                record.setCoTotalMass(CoTotalMass);
//            }
//            if(CoAxleNumber > 0) {
//                record.setCoAxleNumber(CoAxleNumber);
//            }
        }
        return AjaxResult.success(record);
    }

    @GetMapping("/distinguishList")
    public TableDataInfo list(WeightRecordDistinguish weightRecordDistinguish)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        weightRecordDistinguish.setAreas(areas);
        weightRecordDistinguish.setManagementCharacters(managementCharacters);
        startPage();
        List<WeightRecordDistinguish> list = weightRecordService.selectWeightRecordComparisonResults(weightRecordDistinguish);

        return getDataTable(list);
    }

    @GetMapping("/exportDistinguishList")
    public AjaxResult exportDistinguishList(WeightRecordDistinguish weightRecordDistinguish)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        weightRecordDistinguish.setAreas(areas);
        weightRecordDistinguish.setManagementCharacters(managementCharacters);
        List<WeightRecordDistinguish> list = weightRecordService.selectWeightRecordComparisonResults(weightRecordDistinguish);
        ExcelUtil<WeightRecordDistinguish> util = new ExcelUtil<WeightRecordDistinguish>(WeightRecordDistinguish.class);
        return util.exportExcel(list, "称重车牌比对");
    }

    @GetMapping("/comparisonStatistics")
    public TableDataInfo comparisonStatistics()
    {
        List<ComparisonStatistics> list = weightRecordService.selectComparisonStatistics();
        return getDataTable(list);
    }

    @GetMapping("/exportComparisonStatistics")
    public AjaxResult exportComparisonStatistics()
    {
        List<ComparisonStatistics> list = weightRecordService.selectComparisonStatistics();
        ExcelUtil<ComparisonStatistics> util = new ExcelUtil<ComparisonStatistics>(ComparisonStatistics.class);
        return util.exportExcel(list, "车牌识别率统计");
    }

    @GetMapping("/getVideoPath")
    public AjaxResult getVideoPath(@Param(value="id") Long id)
    {
        return  weightRecordService.getVideoPath(id);
    }
}
